﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    public class GameBoardProperty
    {
        private const int x = 19;
        private const int y = 15;
        public const int players = 2;
        public static Tile[,] tiles = new Tile[x, y];
        public static Player[] Players = new Player[players];
        public static Tile SelectedTile;
        public static List<Tile> ReachTiles = new List<Tile>();
        public static int PlayerControlIndex;

        public static int CheckDistance(Vector2 origin, Vector2 destination)
        {
            if (origin.X == destination.X || origin.Y == destination.Y)
            {
                return (int)Math.Max(Math.Abs(origin.X - destination.X), Math.Abs(origin.Y - destination.Y));
            }
            else
            {
                if (origin.Y % 2 == 0)
                {
                    if (destination.Y < origin.Y)
                    {
                        if (destination.X < origin.X)
                        {
                            return 1 + CheckDistance(new Vector2(origin.X - 1, origin.Y - 1), destination);
                        }
                        else
                        {
                            return 1 + CheckDistance(new Vector2(origin.X, origin.Y - 1), destination);
                        }
                    }
                    else
                    {
                        if (destination.X < origin.X)
                        {
                            return 1 + CheckDistance(new Vector2(origin.X - 1, origin.Y + 1), destination);
                        }
                        else
                        {
                            return 1 + CheckDistance(new Vector2(origin.X, origin.Y + 1), destination);
                        }
                    }
                }
                else
                {
                    if (destination.Y < origin.Y)
                    {
                        if (destination.X > origin.X)
                        {
                            return 1 + CheckDistance(new Vector2(origin.X + 1, origin.Y - 1), destination);
                        }
                        else
                        {
                            return 1 + CheckDistance(new Vector2(origin.X, origin.Y - 1), destination);
                        }
                    }
                    else
                    {
                        if (destination.X > origin.X)
                        {
                            return 1 + CheckDistance(new Vector2(origin.X + 1, origin.Y + 1), destination);
                        }
                        else
                        {
                            return 1 + CheckDistance(new Vector2(origin.X, origin.Y + 1), destination);
                        }
                    }
                }
            }
        }
        public static void AllReachTiles(Tile ori, Unit unit, int moveRemaining)
        {
            ReachTiles.Add(ori);

            if (moveRemaining == 0)
            {
                return;
            }

            if (ori._Position.Y % 2 == 0)
            {
                if (ClickBoard(new Vector2((int)ori._Position.X + 1, (int)ori._Position.Y)) && tiles[(int)ori._Position.X + 1, (int)ori._Position.Y]._LandPossible &&  tiles[(int)ori._Position.X + 1, (int)ori._Position.Y].Unit == null)
                {
                    AllReachTiles(tiles[(int)ori._Position.X + 1, (int)ori._Position.Y], unit, moveRemaining - 1);
                }

                if (ClickBoard(new Vector2((int)ori._Position.X, (int)ori._Position.Y + 1)) && tiles[(int)ori._Position.X, (int)ori._Position.Y + 1]._LandPossible &&  tiles[(int)ori._Position.X, (int)ori._Position.Y + 1].Unit == null)
                {
                    AllReachTiles( tiles[(int)ori._Position.X, (int)ori._Position.Y + 1], unit, moveRemaining - 1);
                }

                if (ClickBoard(new Vector2((int)ori._Position.X - 1, (int)ori._Position.Y + 1)) && tiles[(int)ori._Position.X - 1, (int)ori._Position.Y + 1]._LandPossible && tiles[(int)ori._Position.X - 1, (int)ori._Position.Y + 1].Unit == null)
                {
                    AllReachTiles(tiles[(int)ori._Position.X - 1, (int)ori._Position.Y + 1], unit, moveRemaining - 1);
                }

                if (ClickBoard(new Vector2((int)ori._Position.X - 1, (int)ori._Position.Y)) &&  tiles[(int)ori._Position.X - 1, (int)ori._Position.Y]._LandPossible &&  tiles[(int)ori._Position.X - 1, (int)ori._Position.Y].Unit == null)
                {
                    AllReachTiles( tiles[(int)ori._Position.X - 1, (int)ori._Position.Y], unit, moveRemaining - 1);
                }

                if (ClickBoard(new Vector2((int)ori._Position.X - 1, (int)ori._Position.Y - 1)) &&  tiles[(int)ori._Position.X - 1, (int)ori._Position.Y - 1]._LandPossible &&  tiles[(int)ori._Position.X - 1, (int)ori._Position.Y - 1].Unit == null)
                {
                    AllReachTiles( tiles[(int)ori._Position.X - 1, (int)ori._Position.Y - 1], unit, moveRemaining - 1);
                }

                if (ClickBoard(new Vector2((int)ori._Position.X, (int)ori._Position.Y - 1)) &&  tiles[(int)ori._Position.X, (int)ori._Position.Y - 1]._LandPossible &&  tiles[(int)ori._Position.X, (int)ori._Position.Y - 1].Unit == null)
                {
                    AllReachTiles( tiles[(int)ori._Position.X, (int)ori._Position.Y - 1], unit, moveRemaining - 1);
                }
            }
            else
            {
                if (ClickBoard(new Vector2((int)ori._Position.X + 1, (int)ori._Position.Y)) &&  tiles[(int)ori._Position.X + 1, (int)ori._Position.Y]._LandPossible &&  tiles[(int)ori._Position.X + 1, (int)ori._Position.Y].Unit == null)
                {
                    AllReachTiles( tiles[(int)ori._Position.X + 1, (int)ori._Position.Y], unit, moveRemaining - 1);
                }

                if (ClickBoard(new Vector2((int)ori._Position.X + 1, (int)ori._Position.Y + 1)) &&  tiles[(int)ori._Position.X + 1, (int)ori._Position.Y + 1]._LandPossible &&  tiles[(int)ori._Position.X + 1, (int)ori._Position.Y + 1].Unit == null)
                {
                    AllReachTiles( tiles[(int)ori._Position.X + 1, (int)ori._Position.Y + 1], unit, moveRemaining - 1);
                }

                if (ClickBoard(new Vector2((int)ori._Position.X, (int)ori._Position.Y + 1)) &&  tiles[(int)ori._Position.X, (int)ori._Position.Y + 1]._LandPossible &&  tiles[(int)ori._Position.X, (int)ori._Position.Y + 1].Unit == null)
                {
                    AllReachTiles( tiles[(int)ori._Position.X, (int)ori._Position.Y + 1], unit, moveRemaining - 1);
                }

                if (ClickBoard(new Vector2((int)ori._Position.X - 1, (int)ori._Position.Y)) &&  tiles[(int)ori._Position.X - 1, (int)ori._Position.Y]._LandPossible &&  tiles[(int)ori._Position.X - 1, (int)ori._Position.Y].Unit == null)
                {
                    AllReachTiles( tiles[(int)ori._Position.X - 1, (int)ori._Position.Y], unit, moveRemaining - 1);
                }

                if (ClickBoard(new Vector2((int)ori._Position.X, (int)ori._Position.Y - 1)) &&  tiles[(int)ori._Position.X, (int)ori._Position.Y - 1]._LandPossible &&  tiles[(int)ori._Position.X, (int)ori._Position.Y - 1].Unit == null)
                {
                    AllReachTiles( tiles[(int)ori._Position.X, (int)ori._Position.Y - 1], unit, moveRemaining - 1);
                }

                if (ClickBoard(new Vector2((int)ori._Position.X + 1, (int)ori._Position.Y - 1)) &&  tiles[(int)ori._Position.X + 1, (int)ori._Position.Y - 1]._LandPossible &&  tiles[(int)ori._Position.X + 1, (int)ori._Position.Y - 1].Unit == null)
                {
                    AllReachTiles( tiles[(int)ori._Position.X + 1, (int)ori._Position.Y - 1], unit, moveRemaining - 1);
                }
            }
        }
        public static bool ClickBoard(Vector2 position)
        {
            if (position.X >= 0 && position.X < tiles.GetLength(0) && position.Y >= 0 && position.Y < tiles.GetLength(1))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static int CheckDamage(Unit unit1, Unit unit2)
        {
            return 50;
        }
    }
}
